package practice;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class IOPractice {
    public static void main(String[] args) throws IOException {
        //第一遍
        //单字节写
//        FileOutputStream fos = new FileOutputStream("abc.txt");
//        fos.write(-2);
//        fos.write(2);
//        System.out.println("写入完毕");
//        fos.close();
//
//        //单字节读
//        FileInputStream fis = new FileInputStream("abc.txt");
//        int a = fis.read();
//        System.out.println(a);
//        a = fis.read();
//        System.out.println(a);
//        a = fis.read();
//        System.out.println(a);
//        System.out.println("读完了");
//        fis.close();
//
//        //单字节文件复制
//        FileInputStream fis1 = new FileInputStream("map.jpg");
//        FileOutputStream fos1 = new FileOutputStream("map_cp.jpg");
//        int d;
//        while((d=fis1.read())!=-1) {
//               fos1.write(d);
//        }
//        System.out.println("复制完了");
//        fis1.close();
//        fos1.close();
//
//        //块独写的文件复制
//        FileInputStream fis2 = new FileInputStream("map.jpg");
//        FileOutputStream fos2 = new FileOutputStream("map_cp2.jpg");
//        byte[] data = new byte[10*1024];
//        int len;
//        while((len = fis2.read(data))!=-1) {
//            fos2.write(data,0,len);
//        }
//        System.out.println("又复制完了");
//        fis2.close();
//        fos2.close();
//
//        //写文本数据
//        FileOutputStream fos3 = new FileOutputStream("aaa.txt");
//        fos3.write("我在写作业".getBytes(StandardCharsets.UTF_8));
//        System.out.println("写完了");
//        fos3.close();
//
//        //读文本数据
//        File file = new File("aaa.txt");
//        FileInputStream fis3 = new FileInputStream(file);
//        byte[] data3 = new byte[(int)file.length()];
//        fis3.read(data3);
//        String str3 = new String(data3,StandardCharsets.UTF_8);
//        System.out.println(str3);
//        fis3.close();

        //第二遍
        //单字节写
        FileOutputStream fos4 = new FileOutputStream("ccc.txt");
        fos4.write(1);
        System.out.println("写入完毕");
        fos4.close();

        //单字节读
        FileInputStream fis4 = new FileInputStream("ccc.txt");
        int b = fis4.read();
        System.out.println(b);
        b = fis4.read();
        System.out.println(b);
        System.out.println("读完了");
        fis4.close();

        //单字节文件复制
        FileInputStream fis5 = new FileInputStream("map.jpg");
        FileOutputStream fos5 = new FileOutputStream("map1.jpg");
        int d;
        while((d = fis5.read())!=-1) {
            fos5.write(d);
        }
        System.out.println("复制完了");
        fis5.close();
        fos5.close();

        //块独写的文件复制
        FileInputStream fis6 = new FileInputStream("map.jpg");
        FileOutputStream fos6 = new FileOutputStream("map1_cp.jpg");
        byte[] data = new byte[10*1024];
        while(fis6.read(data)!=-1) {
            fos6.write(data);
        }
        System.out.println("复制完毕");
        fis6.close();
        fos6.close();

        //写文本数据
        FileOutputStream fos7 = new FileOutputStream("xxx.txt");
        fos7.write("我在写第二遍作业".getBytes(StandardCharsets.UTF_8));
        System.out.println("写完了");
        fos7.close();

        //读文本数据
        File file = new File("xxx.txt");
        FileInputStream fis7 = new FileInputStream(file);
        byte[] data1 = new byte[(int)file.length()];
        fis7.read(data1);
        String str = new String(data1,StandardCharsets.UTF_8);
        System.out.println(str);
        fis7.close();





    }
}
